cd "$replication\code\"

set matsize 5000

****************************************************************************** Input variables ************************************************************
global cluster "sd_code"
global AS_infs `""""'
global AS_var "NA"
global sample "year!=."
global controls ""
global controlsXYFEs ""
global controlsXinfs ""
global omitted_year1=-1
global omitted_year2=-6
global min_year=-6
global max_year=5
global colour="no_graphs"
global depvar "icw_irrigation"

global int "elecXroad"
local inf1=substr("${int}",1,4)
local inf2=substr("${int}",6,.)
global  infs "`inf1' `inf2' ${int}"
global absorb "shrid_code i.s_code#i.year"
global depvar "icw_irrigation"


global sample "icw_irrigation_2005<=-.374"
qui: do Z_Event_code.do
foreach inf in $infs {
	qui: estimates restore post_`inf'
	qui: estimates store base_1_`inf'
}



global sample "icw_irrigation_2005>-.374"
qui: do Z_Event_code.do
foreach inf in $infs {
	qui: estimates restore post_`inf'
	qui: estimates store base_2_`inf'
}


global sample "rabi_irr_prod_share<=.05 & rabi_irr_prod_share!=."
qui: do Z_Event_code.do
foreach inf in $infs {
	qui: estimates restore post_`inf'
	qui: estimates store het_1_`inf'
}



global sample "rabi_irr_prod_share>.05 & rabi_irr_prod_share!=."
qui: do Z_Event_code.do
foreach inf in $infs {
	qui: estimates restore post_`inf'
	qui: estimates store het_2_`inf'
}




global sample "rabi_irr_prod_share>.05 & rabi_irr_sold_share<.5*rabi_irr_prod_share & rabi_irr_prod_share!=."
qui: do Z_Event_code.do
foreach inf in $infs {
	qui: estimates restore post_`inf'
	qui: estimates store het_3_`inf'
}

global sample "rabi_irr_prod_share>.05 & rabi_irr_sold_share>.5*rabi_irr_prod_share &  rabi_irr_sold_share!=."
qui: do Z_Event_code.do
qui: estimates restore post_elecXroad
foreach inf in $infs {
	qui: estimates restore post_`inf'
	qui: estimates store het_4_`inf'
}



	use "$replication\data\for_event_elecXroad.dta", clear
	sum tdist_10, det
	local med=r(p50)
	global sample "tdist_10>`med'"
	qui: do Z_Event_code.do
	foreach inf in $infs {
		qui: estimates restore post_`inf'
		qui: estimates store far_`inf'
	}


	
	global sample "tdist_10<=`med'"
	qui: do Z_Event_code.do
	foreach inf in $infs {
		qui: estimates restore post_`inf'
		qui: estimates store near_`inf'
	}
	

	

foreach est in het_1 het_2 het_3 het_4 near far  base_1 base_2 {
	estimates table `est'*, stats(N) star(.1 .05 .01) 
}


foreach est in het_1 het_2 het_3 het_4 near far  base_1 base_2{
	estimates restore `est'_elecXroad
	global b_`est'=e(b)[1,1]
	global V_`est'=e(V)[1,1]
	global N_`est'=e(N)
}


clear
set obs 8

gen var=""
gen b=.
gen V=.
gen N=.
local i=0

foreach var in het_1 het_2 het_3 het_4 near far  base_1 base_2 {
    local i=`i'+1
	replace var="`var'" if _n==`i'
	replace b=${b_`var'} if _n==`i'
	replace V=${V_`var'} if _n==`i'
	replace N=${N_`var'} if _n==`i'
}

cap program drop mystringsort 
program mystringsort 
	version 8 
	gettoken varname 0 : 0 
	capture confirm string var `varname' 
	if _rc { 
		di as err "`varname' should be a string variable" 
		exit 198 
	} 	

	tempvar encoded 
	tempname lblname
	local i = 1 
	
	foreach w of local 0 { 
		label def `lblname' `i++' `"`w'"', modify 	
	} 

	encode `varname', gen(`encoded') label(`lblname') 
	sort `encoded' 
end 	

gen ub=b+1.96*sqrt(V)
gen lb=b-1.96*sqrt(V)

global rename_list `"het_1="Districts without irrigated rabi production" het_2="Districts with irrigated rabi production" het_3="Districts where irrigated rabi production mostly consumed" het_4="Districts where irrigated rabi production mostly sold"  near="Villages close to a town" far="Villages far from a town" base_1="Villages with below average dry season cropping" base_2="Villages with above average dry season cropping" "'

mystringsort var het_1 het_2 het_3  het_4 far  near  base_1 base_2
 
mkmat b lb ub, rownames(var) matrix(A)

matrix AT=A'
local colour purple
coefplot matrix(AT), ci((2 3)) xscale(range(0 (.01) .12)) xlabel(0 (.02) .12) xtitle(" " "Coefficient on Electricity and Road interaction") headings("Districts without irrigated rabi production"  = "{bf:(a) Suitability for irrigation in rabi season}"  "Districts where irrigated rabi production mostly consumed" = "{bf:(b) Use of irrigated rabi production}"  "Villages far from a town" = "{bf:(c) Distance to nearest town}"  "Villages with below average dry season cropping" = "{bf:(d) Dry season cropping in 2005}"  ) rename($rename_list)  graphregion(color(white))  mcolor(`colour'*1.5) ciopts(lcolor(`colour'*2)) xline(0) 

graph export "..\Output\het_plot.png", replace

